//
//  ViewController.m
//  UI18Homework
//
//  Created by dllo on 16/8/8.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "ViewController.h"
#import "Contact.h"
#import "ContactTableViewCell.h"
#import "AddContactViewController.h"
#import "ContactDetailViewController.h"
#import "SQLiteDataBaseManager.h"
#define kTableViewReuse @"reuse"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, AddContactViewControllerDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray<Contact *> *contactArray;
@property (nonatomic, strong) SQLiteDataBaseManager *dataBaseManager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.contactArray = [NSMutableArray array];
    self.dataBaseManager = [SQLiteDataBaseManager shareManager];
    [self selectData];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction:)];
    [self creatTableView];
}

- (void)selectData {
    [_dataBaseManager openSQLite3];
    [self.contactArray addObjectsFromArray:[_dataBaseManager selectAll]];
    [_dataBaseManager closeSQLite3];
}

- (void)addAction:(UIBarButtonItem *)barButtonItem {
    AddContactViewController *addVC = [[AddContactViewController alloc] init];
    addVC.delegate = self;
    [self.navigationController pushViewController:addVC animated:YES];
}

- (void)creatTableView {
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)) style:UITableViewStylePlain];
    [self.view addSubview:_myTableView];
    _myTableView.rowHeight = 64;
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [_myTableView registerNib:[UINib nibWithNibName:@"ContactTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kTableViewReuse];
}
#pragma mark - AddContactVC Delegate
- (void)addContact:(Contact *)contact {
    [_contactArray addObject:contact];
    [_myTableView reloadData];
}
#pragma mark - TableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ContactDetailViewController *contactDetailVC = [[ContactDetailViewController alloc] init];
    contactDetailVC.contact = _contactArray[indexPath.row];
    [self.navigationController pushViewController:contactDetailVC animated:YES];
}
#pragma mark - TableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _contactArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ContactTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse];
    Contact *contact = _contactArray[indexPath.row];
    cell.contact = contact;
    return cell;
}
#pragma mark - Memory Warning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
